<html>
<head><meta charset="utf-8"><title>ra_syntax ast traits · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html">ra_syntax ast traits</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="159331318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/159331318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vipentti <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#159331318">(Feb 25 2019 at 11:51)</a>:</h4>
<p>Hey!</p>
<p>Question regarding the <code>ast</code> traits defined in <code>ra_syntax/src/ast.rs</code>, such as <code>NameOwner</code>. At what point do you consider adding a new trait ?</p>
<p>This came up with <a href="https://github.com/rust-analyzer/rust-analyzer/issues/897" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/issues/897">rust-analyzer#897</a> when working with ast::ConstDef &amp; ast::StaticDef, both have a <code>type_ref</code> option, which also appear in multiple other nodes as well. Is there a reason there is no <code>TypeRefOwner</code> trait or could I add one and replace the options <code>TypeRef</code> in <code>grammar.ron</code> with trait <code>TypeRefOwner</code> and then use <code>TypeRefOwner</code> in the trait bounds?</p>
<p>Similar method of working with nodes with <code>NameOwner</code> and a <code>type_ref</code> occurs in <a href="https://github.com/rust-analyzer/rust-analyzer/blob/cec637a3a91f0a34989dfde15ea853bbe8dc8a53/crates/ra_ide_api_light/src/structure.rs#L48" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/blob/cec637a3a91f0a34989dfde15ea853bbe8dc8a53/crates/ra_ide_api_light/src/structure.rs#L48">https://github.com/rust-analyzer/rust-analyzer/blob/cec637a3a91f0a34989dfde15ea853bbe8dc8a53/crates/ra_ide_api_light/src/structure.rs#L48</a></p>



<a name="159331906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/159331906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#159331906">(Feb 25 2019 at 12:03)</a>:</h4>
<p><span class="user-mention" data-user-id="204303">@vipentti</span> in general it's always a good idea to add traits to share AST fuctionlity</p>



<a name="159331969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/159331969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#159331969">(Feb 25 2019 at 12:04)</a>:</h4>
<p>In this case all things with <code>: Type</code> should probably share a trait</p>



<a name="159332000"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/159332000" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#159332000">(Feb 25 2019 at 12:05)</a>:</h4>
<p>I'd say something like</p>
<div class="codehilite"><pre><span></span>trait TypeAscriptionOwner {
    fn ascribed_type(&amp;self) -&gt; Option&lt;TypeRef&gt;;
}

impl TypeAscriptionOwner for const, static, let, param
</pre></div>



<a name="161351519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/161351519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vipentti <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#161351519">(Mar 21 2019 at 14:42)</a>:</h4>
<p>Is there a reason there is no <code>ast::WherePred</code> and a collection for <code>WhereClause</code> of predicates ?</p>



<a name="161351717"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/161351717" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#161351717">(Mar 21 2019 at 14:44)</a>:</h4>
<p>no, there's no reason</p>



<a name="161351732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/161351732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#161351732">(Mar 21 2019 at 14:44)</a>:</h4>
<p>they should be added to grammar.ron</p>



<a name="161351797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/161351797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#161351797">(Mar 21 2019 at 14:45)</a>:</h4>
<p>I am also passively thinking about rewriting AST bits of grammar.ron using a BNF-ish concrete sytnax</p>



<a name="161351901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/161351901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#161351901">(Mar 21 2019 at 14:46)</a>:</h4>
<div class="codehilite"><pre><span></span>fn_def ::=
   modifiers &#39;fn&#39; name param_list return_type? body?
</pre></div>



<a name="161352034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/161352034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vipentti <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#161352034">(Mar 21 2019 at 14:47)</a>:</h4>
<p>That sounds pretty cool actually</p>



<a name="161352208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/161352208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vipentti <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#161352208">(Mar 21 2019 at 14:48)</a>:</h4>
<p>I'll add a <code>WherePred</code> ,  I'm not sure though what methods or traits to implement for it, currently don't need anything but I wonder what would be the best way to get access to the paths</p>



<a name="161352683"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/161352683" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vipentti <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#161352683">(Mar 21 2019 at 14:53)</a>:</h4>
<p>hmm, is there an actual name for the "type" part of the where clause, e.g. <code>T</code> in <code>where T: Debug + Clone</code></p>



<a name="161352763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/161352763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#161352763">(Mar 21 2019 at 14:53)</a>:</h4>
<p>I'd call it just <code>type_ref</code></p>



<a name="161352810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/161352810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vipentti <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#161352810">(Mar 21 2019 at 14:54)</a>:</h4>
<p>I guess the <code>Debug + Clone</code> is called bounds ?</p>



<a name="161352833"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/161352833" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vipentti <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#161352833">(Mar 21 2019 at 14:54)</a>:</h4>
<p>Yeah, that's what I was thinking</p>



<a name="161352863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/161352863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#161352863">(Mar 21 2019 at 14:54)</a>:</h4>
<p>yep, thouse are bounds</p>



<a name="161354085"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax%20ast%20traits/near/161354085" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vipentti <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ra_syntax.20ast.20traits.html#161354085">(Mar 21 2019 at 15:07)</a>:</h4>
<p>hmm, can bounds actually be anything other than <code>PathType</code> ?  or should they return <code>Option&lt;&amp;TypeRef&gt;</code> as well ?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>